/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package detonadores;

import Controladores.ControladorInsertar;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Augusto Andrade
 */
@WebServlet(name = "ProcesarInstitucion", urlPatterns = {"/ProcesarInstitucion"})
public class ProcesarInstitucion extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            ControladorInsertar ControlInstitucion=new ControladorInsertar();
            String patronNumerico = "([0-9]+)";
            String DatoInstitucion=request.getParameter("DatoInstitucion");
            String DatoLugar=request.getParameter("DatoLugar");
            String InsertarIns=null;
            String Evalua="0";
            int indiceLugar = 0;
            // Divisor de cadena         
        int j = 0;
        String [] campos = DatoLugar.split("-");
        
        while(j<campos.length){
        if(j==1){
        Evalua=campos[j];
        }    
        j++;
        }
        // Finaliza Divisor de cadena
        Pattern p2 = Pattern.compile(patronNumerico);
        Matcher matcherNumero = p2.matcher(Evalua);
        if(matcherNumero.matches()){
        indiceLugar=Integer.parseInt(Evalua);    
        if(indiceLugar==0){
        out.println("<script type='text/javascript'>"
        +"alert('La información ingresada en el campo (Ingresa municipio o ciudad de ubicación colegio) no es correcta.');"
        +"pagina='FormularioInstitucion.jsp';"
        +"function redireccionar(){location.href=pagina;}" 
        +"setTimeout ('redireccionar()', 0);"
        +"</script>");
        }else{
        if(DatoInstitucion.equals("Ingresar")){
        out.println("<script type='text/javascript'>"
        +"alert('No se ingreso ningun Colegio');"
        +"pagina='FormularioInstitucion.jsp';"
        +"function redireccionar(){location.href=pagina;}" 
        +"setTimeout ('redireccionar()', 0);"
        +"</script>");    
        }else{
        InsertarIns="0,'"+DatoInstitucion.toUpperCase()+"',"+indiceLugar+"";
        ControlInstitucion.setContenidoInstitucion(InsertarIns);
        ControlInstitucion.IngresarInstitucion();
        if(ControlInstitucion.getContenidoInstitucion()==true){
        out.println("<script type='text/javascript'>"
        +"alert('El Colegio se ingresó Correctamente');"
        +"pagina='FormularioInstitucion.jsp';"
        +"function redireccionar(){location.href=pagina;}" 
        +"setTimeout ('redireccionar()', 0);"
        +"</script>");
        }else{
        out.println("<script type='text/javascript'>"
        +"alert('El Colegio NO se ingresó Correctamente vuelva a intentarlo');"
        +"pagina='FormularioInstitucion.jsp';"
        +"function redireccionar(){location.href=pagina;}" 
        +"setTimeout ('redireccionar()', 0);"
        +"</script>");
        }
        }
        }
        }else{    
        out.println("<script type='text/javascript'>"
        +"alert('La información ingresada en el campo (Ingresa municipio o ciudad de ubicación colegio) no es correcta.');"
        +"pagina='FormularioInstitucion.jsp';"
        +"function redireccionar(){location.href=pagina;}" 
        +"setTimeout ('redireccionar()', 0);"
        +"</script>");
        }
        
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
